package com.ccb.jyzx.expand.ouapi.service.impl;

import com.ccb.jyzx.expand.ouapi.beans.DepartmentInfo;
import com.ccb.jyzx.expand.ouapi.beans.DutyInfo;
import com.ccb.jyzx.expand.ouapi.beans.UserDutyRef;
import com.ccb.jyzx.expand.ouapi.beans.sync.DutyInfoSync;
import com.ccb.jyzx.expand.ouapi.dao.ouapi.DepartmentDao;
import com.ccb.jyzx.expand.ouapi.dao.ouapi.DutyInfoDao;
import com.ccb.jyzx.expand.ouapi.service.DutyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XIEFEI
 * @date 2019-11-13 14:11
 * @Description
 **/
@Service
public class DutyInfoServiceImpl implements DutyService {
    @Autowired
    DutyInfoDao dutyInfoDao;

    @Autowired
    UserDutyRefServiceImpl userInfoDutyInfoRefService;

    @Autowired
    DepartmentDao departmentDao;

    @Override
    public DutyInfo getById(String id) {
        return dutyInfoDao.getById(id);
    }

    @Override
    public List<DutyInfo> getAll() {
        return  dutyInfoDao.getAll();
    }

    @Override
    public List<DutyInfo> getByDepId(String depId) {
        return dutyInfoDao.getByDepId(depId);
    }

    @Override
    public List<DutyInfo> getByUserId(String userId) {

        List<UserDutyRef> refs = userInfoDutyInfoRefService.findByUserId(userId);
        List<DutyInfo> result = new ArrayList<>();
        if (refs!=null&&!refs.isEmpty()){
            refs.forEach(rf->{
                DutyInfo byId = dutyInfoDao.getById(rf.getOrgDeptDutyId());
                if (byId!=null){
                    byId.setFlag(rf.getFlag());
                    String orgDeptId = byId.getOrgDeptId();
                    if (StringUtils.isNotBlank(orgDeptId)){
                        DepartmentInfo departmentInfo = departmentDao.getById(orgDeptId);
                        if (departmentInfo!=null){
                            byId.setDepartmentInfo(departmentInfo);
                        }
                    }
                    result.add(byId);
                }
            });
        }
        return result;
    }

    @Override
    public List<DutyInfoSync> getAllSync() {
        return dutyInfoDao.getAllSync();
    }

    @Override
    public void insert(DutyInfoSync d) {
        dutyInfoDao.save(d);
    }

    @Override
    public void update(DutyInfoSync d) {
        dutyInfoDao.update(d);
    }

    @Override
    public void deleteById(String id) {
        dutyInfoDao.deleteById(id);
    }


}
